﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Business.Entities
{
    [Serializable]
    public class LoginErrorPackage : ErrorPackage
    {
        /// <summary>
        /// Debug data
        /// </summary>
        public int LastCheckPoint = -1;
        public string OutputLog;
        public List<HttpLog> HttpLogs;
        public List<int> CheckPointHistory;

        public void SetException(Exception ex)
        {
            ExceptionStackTrace = ex.StackTrace;
            ExceptionMessage = ex.Message;
        }

        public LoginErrorPackage()
        {
            Reset();
        }

        public void AddCheckPoint(int checkPoint)
        {
            LastCheckPoint = checkPoint;
            CheckPointHistory.Add(checkPoint);
        }

        public string SerializeToString()
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                var xs = new XmlSerializer(typeof (LoginErrorPackage));
                xs.Serialize(sw, this);
                sw.Close();
            }
            return sb.ToString();
        }

        internal void Reset()
        {
            LastCheckPoint = -1;
            HttpLogs = new List<HttpLog>();
            CheckPointHistory = new List<int>();
        }
        
        public void AddHttpLog(HttpLog log)
        {
            HttpLogs.Add(log);
        }

        public HttpLog AddNewHttpLog(string url, int checkPoint)
        {
            HttpLog log = new HttpLog(url, checkPoint);
            HttpLogs.Add(log);
            return log;
        }

        public HttpLog AddNewHttpLog(string url, CookieCollection cookiesIn, int checkPoint)
        {
            HttpLog log = new HttpLog(url, checkPoint);
            log.CookiesIn = cookiesIn.CloneShallow();
            HttpLogs.Add(log);
            return log;
        }

    }

    [Serializable]
    public class HttpLog
    {
        public string Url;
        public int Checkpoint;
        public CookieCollection CookiesIn;
        public CookieCollection CookiesOut;
        public List<NameValuePair> HeadersIn;
        public List<NameValuePair> HeadersOut;
        private string _html;

        [XmlElement("HtmlData")]
        public XmlCDataSection HtmlData
        {
            get
            {
                if (_html == null) return null;
                var doc = new XmlDocument();
                return doc.CreateCDataSection(_html);
            }
            set { _html = value.Value; }
        }

        public void SetHtml(string html)
        {
            _html = html;
        }

        public HttpLog()
        {
        }
        public HttpLog(string url, int checkPoint)
        {
            Url = url;
            Checkpoint = checkPoint;
        }
        private void SetHeaders(WebHeaderCollection col, List<NameValuePair> local)
        {
            foreach (string key in col.AllKeys)
            {
                var pair = new NameValuePair { Name = key, Value = col[key] };
                local.Add(pair);
            }
        }

        public void SetHeadersIn(HttpWebRequest req)
        {
            if (HeadersIn == null) HeadersIn = new List<NameValuePair>();
            SetHeaders(req.Headers, HeadersIn);
        }

        public void SetHeadersOut(WebResponse res)
        {
            if (HeadersOut == null) HeadersOut = new List<NameValuePair>();
            SetHeaders(res.Headers, HeadersOut);
        }
    }

    [Serializable]
    public class NameValuePair
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }

}
